import 'dart:async';

import 'package:lyf_shop/http/core/dio_adapter.dart';
import 'package:lyf_shop/http/core/lyf_error.dart';
import 'package:lyf_shop/http/core/lyf_net_adapter.dart';
import 'package:lyf_shop/http/request/base_request.dart';

class LyfNet {
  /// 单例模式
  LyfNet._();
  static final LyfNet instance = LyfNet._();

  Future request<T>(BaseRequest request) async {
    print('url=${request.url()}');

    /// 这里可以添加header 数据
    request.addHeader("token", "123");

    LyfNetResponse responseData;
    var error;
    try {
      /// 这里 可以使用 各种 Adpter 适配器 处理
      responseData = await send(request);
      return responseData.data;
    } on LyfError catch (e) {
      error = e;
      responseData = e.data;
      throw LyfError(e.code, e.message);
    } catch (e) {
      error = e;
      throw LyfError(-1, '服务开小差了,请别走开');
    }
  }

  Future<dynamic> send<T>(BaseRequest request) async {
    DioAdapter adapter = DioAdapter();
    return adapter.send(request);
  }
}
